package web.config;

import web.common.BaseModifier;
import web.common.CodeEngineConfig;
import org.dom4j.Element;
import org.dom4j.Node;

import java.util.List;
import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: duanbin
 * Date: 2007-8-22
 * Time: 11:11:07
 * To change this template use File | Settings | File Templates.
 */
public class TilesBeanModifier extends BaseModifier {
    public String getFileName() {
        return CodeEngineConfig.getTilesFile();
    }

    public void doModify(String name) {
    //step 1. Add url mapping definitions
        Element tiles = (Element) doc.selectSingleNode("/tiles-definitions");

        String name2 = "dd"; //todo
        String ext = "ssfe.view"; //todo
        getSubNode(tiles, name2, ext);

    }

    private void getSubNode(Element parent, String distKey, String ext){
        Element sub = (Element)parent.selectSingleNode("//definition[@name='"+distKey+"']");
        if(sub == null){
             sub = parent.addElement("definition");
             sub.addAttribute("name", distKey);
             sub.addAttribute("extends", ext);
        }else{
            sub.attribute("extends").setValue(ext);
        }
        String name = "dd"; //todo
        String value = "ssfe.view"; //todo
        String type = "ssfe.view"; //todo
        getSubNode2(sub,name,value,type);
    }

    private void getSubNode2(Element parent, String distKey, String value, String type){
        Element sub = (Element)parent.selectSingleNode("//put[@name='"+distKey+"']");
        if(sub == null){
             sub = parent.addElement("put");
             sub.addAttribute("name", distKey);
             sub.addAttribute("value", value);
             sub.addAttribute("type", type);
        }else{
            sub.attribute("value").setValue(value);
            sub.attribute("type").setValue(type);
        }
    }
}
